package java69.work717_2;

import java.util.Scanner;

public class GoodsInfo {

    static Scanner sc = new Scanner(System.in);
    static Goods[] goods =new Goods[]{
            new Goods(1,"工具书"),
            new Goods(2,"教材书"),
            new Goods(3,"杂志"),
            new Goods(4,"历史传记"),
            new Goods(5,"小说"),
    } ;

    public void   AddType(){
        while (true) {
            boolean pd = true;
            System.out.println("请输入新的类别编号：");
            int id = sc.nextInt();
            System.out.println("请输入新的类别名称：");
            String type = sc.next();
            for (int i = 0; i < goods.length; i++) {
                if (id == goods[i].getId()){
                    pd =false;
                    break;

                }
                if (type.equals(goods[i].getType())){
                    pd =false;
                    break;
                }
                if (pd){
                    goods[goods.length].setId(id);
                    goods[goods.length].setType(type);
                    System.out.println("添加成功！");
                    break;
                }else {
                    System.out.println("添加失败！");
                }
            }
        }
    }

    private String name;//商品名称
    private int num;//商品编号
    private String type;
    private int count;//商品数量
    private double price;//商品价格

    public GoodsInfo(int num,String name,String type,int count,double price) {
        this.count = count;
        this.name = name;
        this.num = num;
        this.price = price;
        this.type = type;
    }
    public GoodsInfo(){

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        if(count<0){
            this.count = 0;
            System.out.println("数量不能低于0");
        }else {
            this.count=count;
        }

    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        if (price<0){
            System.out.println("价格不能低于0");
            this.price = 0;
        }
        this.price = price;
    }

    static GoodsInfo[] goodsInfo = new GoodsInfo[]{
            new GoodsInfo(1001,"百科全书","工具书",10,100),
            new GoodsInfo(4001,"史记","历史传记",100,99),
            new GoodsInfo(3001,"马破苍穹","小说",100,30),
    } ;

public  void  AddGoodSInfo(){
    boolean pd = true;
    boolean pd2=false;
    while (true){
        System.out.println("请输入商品编号：");
        int id2 = sc.nextInt();
        System.out.println("请输入商品名称：");
        String name2 = sc.next();
        System.out.println("请输入商品类别：");
        String type2 = sc.next();
            for (int i = 0; i <goods.length; i++) {
                if (type2.equals(goods[i].getType())){
                    pd2 = true;
                    break;
                }else {
                    pd2 =false;
                }
            }
        System.out.println("请输入商品数量：");
        int count2 = sc.nextInt();
        System.out.println("请输入商品价格：");
        double price2 = sc.nextDouble();
        for (int i = 0; i <goodsInfo.length ; i++) {
            if (id2 == goodsInfo[i].num){
                pd = false;
                break;
            }
            if (name2.equals(goodsInfo[i].name)){
                pd =false;
                break;
            }
        }
        if (pd && pd2){
            System.out.println("输入错误！商品编号/名称已存在或该类型不存在");
        }else {
            System.out.println("添加成功！");
            goodsInfo[goodsInfo.length].setName(name2);
            goodsInfo[goodsInfo.length].setNum(id2);
            goodsInfo[goodsInfo.length].setCount(count2);
            goodsInfo[goodsInfo.length].setPrice(price2);
            goodsInfo[goodsInfo.length].setType(type2);
        }
    }

}

public  void  show(){
    for (int i = 0; i <goodsInfo.length ; i++) {
        System.out.println("编号："+goodsInfo[i].getNum()+"，书名："+goodsInfo[i].getName()+
                "，类型:"+goodsInfo[i].getType()+"，总价："+goodsInfo[i].getNum()*goodsInfo[i].getPrice());
    }

}
}
